let express = require('express');
let session = require('express-session');
let Config =require('./lib/config');
let Util = require ('./lib/util');
let router = require('./router/router');
// let log = require('./lib/log');

let app = express();
//CORS配置
Util.allowCORS(app);
//数据json格式化
app.use(express.json());
//使用session
app.use(session({
    secret:'secret',
    resave:true,
    saveUnitialized:false,
    cookie:{
        maxAge:1000*60*3
    }
}));
//监听用户是否登录
app.use((req, res, next)=>{
    console.log('test')
    //不拦截登录接口
    let is_login = false;
    if(Config.access_routes.includes(req.originalUrl) || is_login){
        next()
    }else {
        res.json(Util.formatErrorRes(401))
    }
});
//路由设置
app.use('/',router);

app.listen(Config.port,function () {
    console.log('服务已启动：running '+Config.port)
})